package function;

import javax.swing.*;

import UserDatabase.UserDatabase;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class UpdateUserFrame extends JFrame{
	//编号，姓名，单元号，房间号，密码，确认密码
	private JLabel l_id,l_name,l_hno,l_password1,l_password2;
	private JLabel l_tip1,l_tip2;
	private JTextField t_id,t_name,t_hno;
	private JPasswordField p_password1,p_password2;
	private JButton b_update,b_reset;//按钮：修改,重置
	
	public UpdateUserFrame() {
		this.setSize(400,620);
		this.setTitle("修改信息");
		this.setLocation(500,200);
		init();
		this.setVisible(true);
	}
	
	public void init() {
		Font fontB = new Font("宋体",Font.BOLD,12);
		l_tip1 = new JLabel("请输入修改信息的编号",JLabel.CENTER);
		l_tip1.setFont(fontB);
		l_tip2 = new JLabel("请重新填写下列信息",JLabel.CENTER);
		l_tip2.setFont(fontB);
		l_id = new JLabel("编号",JLabel.CENTER);
		l_id.setFont(fontB);
		l_name = new JLabel("姓名",JLabel.CENTER);
		l_name.setFont(fontB);
		l_hno = new JLabel("房间号",JLabel.CENTER);
		l_hno.setFont(fontB);
		
		l_password1 = new JLabel("密码",JLabel.CENTER);
		l_password1.setFont(fontB);
		l_password2 = new JLabel("确认密码",JLabel.CENTER);
		l_password2.setFont(fontB);
		
		t_id = new JTextField();
		t_id.setFont(fontB);
		t_name = new JTextField();
		t_name.setFont(fontB);
		t_hno = new JTextField();
		t_hno.setFont(fontB);
		
		
		p_password1 = new JPasswordField();
		p_password1.setFont(fontB);
		p_password2 = new JPasswordField();
		p_password2.setFont(fontB);
		
		b_update = new JButton("修改信息");
		b_update.setFont(fontB);
		b_update.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {
				//从窗体收集用户信息
				String id = t_id.getText();
				String name = t_name.getText();
				String password1 = new String(p_password1.getPassword());
				String password2 = new String(p_password2.getPassword());
				String hno = t_hno.getText();
				
				JFrame f = new JFrame();
				if(id.equals("")) {
					JOptionPane.showMessageDialog(f,"编号不能为空！","提示框",JOptionPane.WARNING_MESSAGE);
				}else if(name.equals("")) {
					JOptionPane.showMessageDialog(f,"姓名不能为空！","提示框",JOptionPane.WARNING_MESSAGE);
				}else if(hno.equals("")) {
					JOptionPane.showMessageDialog(f,"房间号不能为空！","提示框",JOptionPane.WARNING_MESSAGE);
				}else if(password1.equals("")||password2.equals("")) {
				
					JOptionPane.showMessageDialog(f,"密码不能为空！","提示框",JOptionPane.WARNING_MESSAGE);
				
				}else if(!password1.equals(password2)) {
					
					JOptionPane.showMessageDialog(f,"两次密码应一致！","提示框",JOptionPane.WARNING_MESSAGE);
						
					p_password1.setText("");
					
					p_password2.setText("");
					
					return ;
				}
				
				
				//用数据库导入数据
				if(!id.equals("")&&!name.equals("")&&!password1.equals("")&&!password2.equals("")&&!hno.equals("")) {
					UserDatabase udb = new UserDatabase();
					
					String SQL = "update user set name = '"+name+"',password = '"+password1+"',hno = '"+hno+"' where id = '"+id+"'";
					
					udb.updata(SQL);
					
					JOptionPane.showMessageDialog(f,"修改成功,刷新页面可显示！","提示框",JOptionPane.WARNING_MESSAGE);
					
					setVisible(false);
				}
			}

		});
		b_reset = new JButton("重置");
		b_reset.setFont(fontB);
		b_reset.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {
				t_id.setText(null);
				t_name.setText(null);
				t_hno.setText(null);
				p_password1.setText(null);
				p_password2.setText(null);
			}
			
		});
		
		this.setLayout(null);
		JPanel p = new JPanel();
		p.setLayout(new GridLayout(1,1,5,5));
		p.add(l_tip1);
		p.setBounds(19,10,350,60);
		this.add(p);
		
		p = new JPanel();
		p.setLayout(new GridLayout(1,2,5,5));
		p.add(l_id);
		p.add(t_id);
		p.setBounds(19,75,350,60);
		this.add(p);
		
		p = new JPanel();
		p.setLayout(new GridLayout(1,1,5,5));
		p.add(l_tip2);
		p.setBounds(19,140,350,60);
		this.add(p);
		
		p = new JPanel();
		p.setLayout(new GridLayout(4,2,5,5));
		
		p.add(l_name);
		p.add(t_name);
		
		p.add(l_hno);
		p.add(t_hno);
		
		p.add(l_password1);
		p.add(p_password1);
		
		p.add(l_password2);
		p.add(p_password2);
		
		p.setBounds(19,205,350,250);
		
		this.add(p);
		
		p = new JPanel();
		p.setLayout(new GridLayout(1,2,5,5));
		p.add(b_update);
		p.add(b_reset);
		
		p.setBounds(19,500,350,30);
		
		this.add(p);
		this.setResizable(false);
		
	}
}
